package com.java.jdbc;

import javax.xml.transform.Result;
import java.sql.*;

public class DriverManagerDemo {
    public static final String url = "jdbc:mysql://localhost:3306/testdb1?serverTimezone=GMT&useUnicode=true&characterEncoding=Utf-8&useSSL=false";

    public static void main(String[] args) {
//        //加载驱动
//        try{
//            Class.forName("com.mysql.cj.jdbc.Driver");
//        } catch (ClassNotFoundException e){
//            e.printStackTrace();
//        }
//        Connection connection = null;
        //获得连接对象
        try(Connection connection = DriverManager.getConnection(url,"root","123456");) {

            System.out.println(connection);
            //获得命令对象
            Statement statement = connection.createStatement();
            System.out.println(statement);
            //执行SQL语句
            //int rs = statement.executeUpdate("insert into student(name,age,gender,address) values ('张三丰',30,'男','武当山')");
            //PreparedStatement preparedStatement = connection.prepareStatement("");
            //执行查询语句，获得结果集
            PreparedStatement preparedStatement = connection.prepareStatement("select * from goods");
            ResultSet resultSet = preparedStatement.executeQuery();
            //循环读取每一行数据
            while(resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Float price = resultSet.getFloat("price");
                String product_address = resultSet.getString("product_address");
                String type = resultSet.getString("type");
                System.out.println(id +"--"+ name + "--"+price+"--"+product_address+"--"+type);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        finally {
//            try {
//                //关闭连接
//                connection.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
    }
}
